import type { Context, Next } from "koa";
import { container } from "@/services/register";

/**
 * 可能登录
 */
export default async (ctx: Context, next: Next) => {
  const token = ctx.headers["authorization"];

  if (token) {
    // Bearer <token> 格式，去掉 "Bearer " 前缀
    const tokenValue = token.split(" ")[1];

    // 将解码后的用户信息附加到请求对象上
    ctx.state.user = container
      .resolve("JsonwebtokenService")
      .verify(tokenValue);
  }

  await next();
};
